﻿using BCCommon;
using BCData.TMS.Contraband.ContrabandCategory;
using BCData.TMS.Contraband.ContrabandConfig;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.Contraband.ContrabandConfig;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.Contraband.ContrabandConfig;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.Contraband.ContrabandConfig
{
    public class ContrabandConfigService : IContrabandConfigService
    {
        #region var
        private readonly IContrabandConfigData contrabandConfigData;
        private readonly IContrabandCategoryData contrabandCategoryData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.ContrabandConfigEntity);
        #endregion

        #region 构造函数

        public ContrabandConfigService(
            IContrabandConfigData contrabandConfigData,
            IContrabandCategoryData contrabandCategoryData,
            IRedisService redisService)
        {
            this.contrabandConfigData = contrabandConfigData;
            this.contrabandCategoryData = contrabandCategoryData;
            this.redisService = redisService;
        }
        #endregion

        #region Add
        /// <summary>
        /// 添加新违禁品配置
        /// </summary>
        /// <param name="admin">管理员信息</param>
        /// <param name="requestDto">违禁品配置数据模型</param>
        /// <returns></returns>
        public ContrabandConfigDto Add(AdminDto admin, ContrabandConfigRequestDto requestDto)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限添加");
            }
            var contrabandCategory = this.contrabandCategoryData.GetEntityAsync(requestDto.ContrabandCategoryId).Result;
            if (contrabandCategory == null)
            {
                throw new ArgumentException("违禁品分类信息不存在");
            }
            var result = this.contrabandConfigData.InsertAsync(new ContrabandConfigEntity()
            {
                ContrabandCategoryId = contrabandCategory.ContrabandCategoryId,
                ContrabandContent = requestDto.ContrabandContent,
                CreateAdminId = admin.AdminId,
                CreateAdminName = admin.FullName,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                EditAdminId = admin.AdminId,
                EditAdminName = admin.FullName
            }).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<ContrabandConfigDto>();
        }
        #endregion

        public ContrabandConfigDto Update(AdminDto admin, int contrabandConfigId, ContrabandConfigRequestDto requestDto)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限修改");
            }
            var contrabandCategory = this.contrabandCategoryData.GetEntityAsync(requestDto.ContrabandCategoryId).Result;
            if (contrabandCategory == null)
            {
                throw new ArgumentException("违禁品分类信息不存在");
            }
            var result = this.contrabandConfigData.GetEntityAsync(contrabandConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("此违禁品配置信息不存在");
            }
            result.ContrabandCategoryId = requestDto.ContrabandCategoryId;
            result.ContrabandContent = requestDto.ContrabandContent;
            result.UpdateTime = DateTime.Now;
            result.EditAdminId = admin.AdminId;
            result.EditAdminName = admin.FullName;
            this.contrabandConfigData.UpdateAsync(result).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<ContrabandConfigDto>();
        }

        public void Delete(AdminDto admin, int contrabandConfigId)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException("您不是总公司员工，暂无权限删除");
            }
            var result = this.contrabandConfigData.GetEntityAsync(contrabandConfigId).Result;
            if (result == null)
            {
                throw new ArgumentException("此违禁品配置信息不存在");
            }
            this.contrabandConfigData.DeleteAsync(contrabandConfigId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }

        public PageableList<ContrabandConfigViewDto> Query(AdminDto admin, int? contrabandCategoryId, DateTime? createStartDate, DateTime? createEndDate, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, contrabandCategoryId, createStartDate, createEndDate, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
             {
                 var (data, dataCount) = this.contrabandConfigData.Query(contrabandCategoryId, createStartDate, createEndDate, pageIndex, pageSize).Result;
                 return new PageableList<ContrabandConfigViewDto>()
                 {
                     Count = dataCount,
                     Items = data.As<List<ContrabandConfigViewDto>>(),
                     PageIndex = pageIndex,
                     PageSize = pageSize
                 };
             }, TimeSpan.FromDays(30));
            return result;
        }

        public IEnumerable<ContrabandConfigViewDto> GetList(int? contrabandCategoryId)
        {
            string cacheKey = this.redisService.GetKey(key, contrabandCategoryId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.contrabandConfigData.GetList(contrabandCategoryId).Result;
                return data.As<List<ContrabandConfigViewDto>>();
            }, TimeSpan.FromDays(1));
            return result;
        }
    }
}
